package controler;

import java.beans.IntrospectionException;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;

import rmi.Discussion;
import rmi.Me;

/**
 * @author REMY Timothée and CARTIER Arthur
 * 
 */
public class xmlReadWrite {

	public static Me restoreMe(final String FileName)
			throws FileNotFoundException, IntrospectionException {
		Me me = null;
		final XMLDecoder decoder = new XMLDecoder(new FileInputStream(FileName));
		me = (Me) decoder.readObject();
		decoder.close();
		return me;
	}

	public static Discussion restoreOneDiscussion(final String FileName)
			throws FileNotFoundException {
		Discussion discussion = null;
		final XMLDecoder decoder = new XMLDecoder(new FileInputStream(FileName));
		discussion = (Discussion) decoder.readObject();
		decoder.close();
		return discussion;
	}

	public static void saveDiscussions(final ArrayList<Discussion> discussions,
			final String FolderName) throws FileNotFoundException {
		for (final Discussion d : discussions) {
			saveOneDiscussion(d, FolderName);
		}
	}

	public static void saveMe(final Me me, final String FileName)
			throws FileNotFoundException, IntrospectionException {

		final XMLEncoder encoder = new XMLEncoder(
				new FileOutputStream(FileName));
		encoder.writeObject(me);
		encoder.flush();
		encoder.close();
	}

	public static void saveOneDiscussion(final Discussion discussion,
			final String FolderName) throws FileNotFoundException {
		final XMLEncoder encoder = new XMLEncoder(new FileOutputStream(
				FolderName + "/" + discussion.getId()));
		encoder.writeObject(discussion);
		encoder.flush();
		encoder.close();
	}

}
